import 'package:flutter/material.dart';

// 封装的一个根据盒子宽高自适应文本大小的组件
class SelfAdaptionText extends StatelessWidget {
  final double? height;
  final double? width;
  final BoxFit fit;
  final String data;
  final TextStyle? style;

  // 将原有构造函数私有化 不让使用者直接使用而是使用命名构造函数
  const SelfAdaptionText._(
    this.data, {
    super.key,
    this.height,
    this.width,
    required this.fit,
    this.style,
  });

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: height,
      width: width,
      child: FittedBox(
        fit: fit,
        child: Text(data, style: style, overflow: TextOverflow.visible,),
      ),
    );
  }

  // 根据宽度自适应
  factory SelfAdaptionText.fitWidth(String data,
      {Key? key, required double width, TextStyle? style}) {
    return SelfAdaptionText._(
      data,
      key: key,
      width: width,
      fit: BoxFit.fitHeight,
      style: style,
    );
  }

  // 根据高度自适应
  factory SelfAdaptionText.fitHeight(String data,
      {Key? key, required double height, TextStyle? style}) {
    return SelfAdaptionText._(
      data,
      key: key,
      height: height,
      fit: BoxFit.fitHeight,
      style: style,
    );
  }

  // 根据高度和宽度自适应
  factory SelfAdaptionText.cover(String data,
      {Key? key,
      required double height,
      required double width,
      TextStyle? style}) {
    return SelfAdaptionText._(
      data,
      key: key,
      height: height,
      width: width,
      fit: BoxFit.cover,
      style: style,
    );
  }

  // 根据高度和宽度自适应
  factory SelfAdaptionText.contain(String data,
      {Key? key,
      required double height,
      required double width,
      TextStyle? style}) {
    return SelfAdaptionText._(
      data,
      key: key,
      height: height,
      width: width,
      fit: BoxFit.contain,
      style: style,
    );
  }
}
